// 定义接口
interface IPerson {
  name: string
  age: number
  friend?: {
    name: string
  }
}

const info: IPerson = {
  name: "why",
  age: 18
}

console.log(info.friend?.name)

// info.friend?.name = "kobe" // 错误

// 解决方案一:类型缩小
if (info.friend) {
  info.friend.name = "kobe"
}

// 解决方案二:非空类型断言(慎重)
info.friend!.name = "james"